package main

import (
	"demo/api"
	handlers "demo/api/handlers"
	appservice "demo/application/service"
	"demo/domain/service"
	"demo/domain/task"
	"demo/infra/common/utils"
	"demo/infra/config"
	"demo/infra/db"
	"demo/infra/middleware/auth"
	"demo/infra/middleware/cache"
	"demo/infra/middleware/transform"
	"demo/infra/persistence/repository"
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func main() {
	// 初始化日志
	logger, _ := zap.NewProduction()
	defer logger.Sync()
	// 初始化配置管理器
	configManager, err := config.NewConfigManager()
	if err != nil {
		logger.Error("Failed to load config: ", zap.Error(err))
	}
	var conf = configManager.GetConfig()
	// 初始化db
	db.Init(conf.Database)
	rs := db.InitRedis(conf.RedisConf)
	//注入 Redis 客户端到 Session Store
	auth.InitRedis(rs)
	// 依赖注入
	r := initInjection(logger, db.DB, rs, conf)
	if err := r.Run(fmt.Sprintf(":%d", conf.Port)); err != nil {
		logger.Error("服务器启动失败 ", zap.Error(err))
	}
}

// initInjection 初始注入依赖
func initInjection(logger *zap.Logger, db *gorm.DB, redis *redis.Client, config *config.Config) *gin.Engine {
	//注入infra层repo
	urlMapRepo := repository.NewUrlMapRepository(db)
	urmMapReplaceRepo := repository.NewUrlMapReplaceRecordRepository(db)
	domainConfRepo := repository.NewDomainConfRepository(db)
	compressCodeRepo := repository.NewCompressCodeRepository(db)
	visitStatisticsRepo := repository.NewVisitStatisticsRepository(db)
	transformEventRecordRepo := repository.NewTransformEventRecordRepository(db)
	userRepository := repository.NewUserRepository(db)
	userLoginRecordRepository := repository.NewUserLoginRecordRepository(db)
	//注入infra层工具
	snowflakeKeyGenerator := utils.NewSnowflakeKeyGenerator(config.DataCenterId, config.DataWorkId)
	urlMapCacheManager := cache.NewUrlMapCacheManager(redis, urlMapRepo)
	//注入domain层service
	urlMapService := service.NewUrlMapService(urlMapRepo, urmMapReplaceRepo, domainConfRepo, compressCodeRepo,
		snowflakeKeyGenerator, urlMapCacheManager, config.Domain, db)
	statisticsService := service.NewStatisticsService(transformEventRecordRepo, visitStatisticsRepo)
	userInfoService := service.NewUserInfoService(userRepository, userLoginRecordRepository, db)
	// 注入domain层task,并执行
	businessTask := task.NewBusinessTask(redis, logger, statisticsService)
	businessTask.InitTask()
	//注入application层service
	convertUrlService := appservice.NewConvertUrlService(urlMapService)
	urlQueryService := appservice.NewUrlQueryService(visitStatisticsRepo, urlMapRepo)
	userService := appservice.NewUserService(userInfoService)
	//注入handler
	convertUrlHandler := handlers.NewConvertUrlHandler(convertUrlService, urlQueryService)
	authHandler := handlers.NewAuthHandler(userService)
	// 注入中间件
	transformMiddleware := transform.NewTransformMiddleware(urlMapCacheManager, transformEventRecordRepo)
	authMiddleware := auth.NewAuthMiddleware()
	//注入路由
	router := api.NewRouter(convertUrlHandler, authHandler, transformMiddleware, authMiddleware)
	return router.SetupRouter()
}
